package com.springboot.redis;

import com.baomidou.mybatisplus.enums.FieldFill;
import com.baomidou.mybatisplus.enums.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;

public class Dudu {
    public static void main(String[] args) {
        //构建一个代码生成实体
        AutoGenerator autoGenerator = new AutoGenerator();

        //配置全局
        GlobalConfig globalConfig = new GlobalConfig();
        String property = System.getProperty("user.dir");
        globalConfig.setOutputDir(property +"/10-codingstudy-redis/src/main/java");
        globalConfig.setAuthor("Dudu");
        globalConfig.setOpen(false); //是否打开文件夹
        globalConfig.setFileOverride(false); //是否覆盖
        globalConfig.setServiceName("%sService"); //去service的“I”前缀
        globalConfig.setIdType(IdType.ID_WORKER);
        autoGenerator.setGlobalConfig(globalConfig);

        //配置数据源
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/testmysql?serverTimezone=UTC");
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("Aa1234");
        dataSourceConfig.setDbType(DbType.MYSQL);
        autoGenerator.setDataSource(dataSourceConfig);

        //包的配置
        PackageConfig packageConfig = new PackageConfig();
        //packageConfig.setModuleName("test1");
        packageConfig.setParent("com.springboot.redis");
        packageConfig.setEntity("modle");
        packageConfig.setController("controller");
        packageConfig.setService("service");
        autoGenerator.setPackageInfo(packageConfig);

        //策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setInclude("tb_user","tb_role","tb_user_role");
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setEntityLombokModel(true);
        strategyConfig.setLogicDeleteFieldName("Deleted");
        strategyConfig.setVersionFieldName("Version");
        strategyConfig.setRestControllerStyle(true);

        TableFill gmtCreate = new TableFill("CreateTime" , FieldFill.INSERT);
        TableFill gmtModified = new TableFill("CreateTime" , FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFillArrayList = new ArrayList<>();
        tableFillArrayList.add(gmtCreate);
        tableFillArrayList.add(gmtModified);
        strategyConfig.setTableFillList(tableFillArrayList);
        autoGenerator.setStrategy(strategyConfig);



        autoGenerator.execute();//执行

    }
}
